<?php
require_once "config.php";

$id = $_POST['id'];
$code = $_POST['code'];
$u_id = $_POST['u_id'];

if($code!=0) {
    $zan_see = $mysql->query("select * from dyspw_like where uid=$u_id and weblinkid=$id");
    $see_rows = $zan_see->fetch_all(MYSQLI_ASSOC);
    if(!$see_rows){
        $sql = $mysql->query("insert into dyspw_like (id,uid,weblinkid,state) values (null,'{$u_id}','{$id}',1)");
        if($sql){
            $web_up = $mysql->query("update dyspw_weblink set zan=zan+1 where id=$id");
            $wweb_sql =  $mysql->query("select * from dyspw_weblink where id={$id}");
            $wweb_rows = $wweb_sql->fetch_all(MYSQLI_ASSOC);
            echo json_encode(['code'=>1,'rows'=>$wweb_rows[0]['zan']]);
        }else{
            print_r("添加失败");die;
        }
    }else{
        $upd_sql = $mysql->query("update dyspw_like set state=1 where uid={$u_id} and weblinkid=$id");

        if($upd_sql){
            $web_up = $mysql->query("update dyspw_weblink set zan=zan+1 where id=$id");
            $wweb_sql =  $mysql->query("select * from dyspw_weblink where id={$id}");
            $wweb_rows = $wweb_sql->fetch_all(MYSQLI_ASSOC);
            echo json_encode(['code'=>1,'rows'=>$wweb_rows[0]['zan']]);
        }else{
            print_r("修改失败");die;
        }
    }

}else{
    $zan_see = $mysql->query("select * from dyspw_like where uid={$u_id} and weblinkid={$id}");
    $see_rows = $zan_see->fetch_all(MYSQLI_ASSOC);
    if($see_rows){
        $sql = $mysql->query("update dyspw_like set state=0 where uid={$u_id} and weblinkid=$id");
        if($sql){
            $web_sql =  $mysql->query("select * from dyspw_weblink where id={$id}");
            $web_rows = $web_sql->fetch_all(MYSQLI_ASSOC);
            if($web_rows[0]['zan']>0){

                $web_up = $mysql->query("update dyspw_weblink set zan=zan-1 where id={$id}");
                $wweb_sql =  $mysql->query("select * from dyspw_weblink where id={$id}");
                $wweb_rows = $wweb_sql->fetch_all(MYSQLI_ASSOC);
                echo json_encode(['code'=>1,'rows'=>$wweb_rows[0]['zan']]);
            }else{
                print_r("点赞量失败");die;
            }
        }
    }

}




?>
